package com.mes.inventory.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityUtils;
import com.mes.basicdata.dao.iface.ITypesDataDao;
import com.mes.common.service.BasicService;
import com.mes.common.util.IdentityGenerator;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.iface.IInvIODao;
import com.mes.inventory.dao.iface.IInvlnfoDao;
import com.mes.inventory.dao.iface.IWarebinDao;
import com.mes.inventory.dao.iface.IWarehouseDao;
import com.mes.inventory.dao.iface.IWarehouseKeeperDao;
import com.mes.inventory.domain.InvIO;
import com.mes.inventory.domain.InvInfo;
import com.mes.inventory.domain.Warebin;
import com.mes.inventory.domain.Warehouse;
import com.mes.inventory.domain.WarehouseKeeper;

@Service
public class WarehouseService extends BasicService<Warehouse> {
	@Autowired
	private SysUtil sysUtil;

	@Autowired
	private IWarehouseDao warehouseDao;

	@Autowired
	private IWarehouseKeeperDao warehouseKeeperDao;

	@Autowired
	private ITypesDataDao typesDataDao;

	@Autowired
	private IInvlnfoDao invInfoDao;

	@Autowired
	private IInvIODao InvIODao;

	@Autowired
	private IWarebinDao warebinDao;

	@DataProvider
	public List<Warehouse> getWarehousesByEmployeeUid(String employeeUid) throws Exception{
		return warehouseDao.getWarehousesByEmployeeUid(SysUtil.getSessionEmployeeUid());
	}
	
	@DataProvider
	public Collection<Warehouse> getWarehouseByEmp() throws Exception {
		String employeeUid = ContextHolder.getLoginUser().getUsername();

		List<WarehouseKeeper> warehouseKeepers = warehouseKeeperDao.findByProperty("employeeUid", employeeUid);

		List<Warehouse> warehouses = new ArrayList<>();
		for (WarehouseKeeper warehouseKeeper : warehouseKeepers) {
			Warehouse warehouse = warehouseKeeper.getWarehouse();
			warehouses.add(warehouse);
		}

		return warehouses;

	}

	// 根据id查询仓库信息
	@DataProvider
	public List<Warehouse> getWarehouseById(String warehouseId) throws Exception {
		// String warehouseId = (String) params.get("warehouseUid");
		List<Warehouse> list = new ArrayList<Warehouse>();
		System.out.println(warehouseId);
		Warehouse warehouse = warehouseDao.getById(warehouseId);
		// 获取库房类型编号;
		String typeId = warehouse.getWarehouseType();
		if (null != typeId && !typeId.equals("")) {
			Object virtualObj = null;
			try {
				// 根据条件查询库房类型描述
				String typeIdDesc = typesDataDao.getTypeIdDescByTypeName("warehousetype", typeId);
				// 将库房类型描述保存至warehouse的warehouseTypeDesc(虚拟属性)中
				virtualObj = EntityUtils.toEntity(warehouse);
				EntityUtils.setValue(virtualObj, "warehouseTypeDesc", typeIdDesc);
			} catch (Exception e) {
				e.printStackTrace();
			}
			warehouse = (Warehouse) virtualObj;
		}
		list.add(warehouse);
		return list;
	}

	@DataProvider
	public Collection<Warehouse> getWarehouseOfThisDept() throws Exception {

		List<Warehouse> list = warehouseDao.getWarehouseByDeptUid(sysUtil.getSessionDeptUid());
		return list;
	}

	@Expose
	public Boolean selectWarehouse(String mWarehouseUid) throws Exception {

		List<InvInfo> list = new ArrayList<InvInfo>();
		List<InvIO> list1 = new ArrayList<InvIO>();

		if (mWarehouseUid != null && mWarehouseUid != "") {

			list = invInfoDao.findByProperty("warehouseUid", mWarehouseUid);
			list1 = InvIODao.findByProperty("warehouseUid", mWarehouseUid);

		}

		if (list != null && list.size() > 0 || list1 != null && list1.size() > 0) {
			return false;
		}

		return true;

	}

	@DataResolver
	public void addWarehouse(Collection<Warehouse> warehouses) throws Exception {
		for (Warehouse warehouse : warehouses) {
			if (warehouse.getUniqueId() == null) {
				warehouse.setUniqueId(IdentityGenerator.GetIdentityUUID());
			}
			String warehouseId = warehouse.getWarehouseId();
			List<Warehouse> list = warehouseDao.findByProperty("warehouseId", warehouseId);
			if (null != list && list.size() > 0) {
				throw new Exception("该库房编号已存在!");
			}
			Warebin warebin = new Warebin();
			// 设置柜位ID
			warebin.setBinID("NA");
			// 设置柜位名称
			warebin.setBinName("默认柜位");
			// 设置库房ID
			warebin.setWarehouseUid(warehouse.getUniqueId());
			// 设置柜位主键ID
			warebin.setWarebinUid(IdentityGenerator.GetIdentityUUID());
			warebinDao.add(warebin);
		}
		warehouseDao.add(warehouses);
	}

	@DataResolver
	public void saveWarehouse(Collection<Warehouse> warehouses) throws Exception {
		for (Warehouse warehouse : warehouses) {
			switch (EntityUtils.getState(warehouse)) {
			case MODIFIED:
				warehouseDao.update(warehouse);
				break;
			case DELETED:
				warehouseDao.delete(warehouse);
				List<Warebin> warebins2 = warebinDao.findByProperty("warehouseUid", warehouse.getUniqueId());
				if (warebins2 != null) {
					warebinDao.delete(warebins2);
				}
				break;
			case NEW:
				this.addWarehouse(warehouses);
				break;
			default:
				break;
			}

		}
	}

}
